/*
 * Copyright (c) 2018, dreamkaylee@foxmail.com All Rights Reserved.
 */

package com.vip.jwt.common.utils;

import javax.servlet.http.HttpServletRequest;
import java.util.ResourceBundle;

/**
 * 项目参数工具类
 *
 * @author limk
 * @date 2018年2月12日 下午6:01:58
 */
public class ResourceUtil {

    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("sys");

    /**
     * 获取项目路径
     *
     * @param request
     * @return
     */
    public static String getPorjectPath(HttpServletRequest request) {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort() + request.getContextPath();
    }

    /**
     * 获取项目根路径
     *
     * @param request
     * @return
     */
    public static String getTomcatPath(HttpServletRequest request) {
        String porjectPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getLocalPort();
        return porjectPath;
    }

    /**
     * 获得请求路径
     *
     * @param request
     * @return
     */
    public static String getRequestPath(HttpServletRequest request) {
        String and = "&";
        String requestPath = request.getRequestURI() + "?" + request.getQueryString();
        // 去掉其他参数
        if (requestPath.contains(and)) {
            requestPath = requestPath.substring(0, requestPath.indexOf("&"));
        }
        // 去掉项目路径
        requestPath = requestPath.substring(request.getContextPath().length() + 1);
        return requestPath;
    }

    /**
     * 获取项目根目录
     *
     * @return
     */
    public static String getPorjectPath() {
        String nowpath; // 当前tomcat的bin目录的路径
        String tempdir;
        nowpath = System.getProperty("user.dir");
        // 把bin文件夹变到 webapps文件里面
        tempdir = nowpath.replace("bin", "webapps");
        // 拼成D:\java\software\apache-tomcat-6.0.14\webapps\sz_pro
        tempdir += "\\";
        return tempdir;
    }

    /**
     * 获取项目静态文件根目录
     *
     * @return
     */
    public static String getSysPath() {
        String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
        String temp = path.replaceFirst("file:/", "").replaceFirst("WEB-INF/classes/", "");
        String separator = System.getProperty("file.separator");
        return temp.replaceAll("/", separator + separator).replaceAll("%20", " ");
    }

    /**
     * 获取项目Class根目录
     *
     * @return
     */
    public static String getClassPath() {
        String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
        String temp = path.replaceFirst("file:/", "");
        String separator = System.getProperty("file.separator");
        return temp.replaceAll("/", separator + separator);
    }

    /**
     * 获取配置文件参数
     *
     * @param name
     * @return
     */
    public static String getConfig(String name) {
        return BUNDLE.getString(name);
    }

}
